package org.anupta.realm.server.inter.client;

import org.anupta.realm.main.Main;
import org.anupta.realm.server.inter.packet.InterPacket;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

public class InterClient {
	private IoSession session = null;
	private InterParser parser = null;
	private byte guid = 0;
	private Type type = Type.CLASSIC;
	private State state = State.OFFLINE;
	private Population pop = Population.FULL;
	private String ip = null;
	private int port = 0;
	
	public InterClient(IoSession session){
		setSession(session);
		setParser(new InterParser(this));
	}
	
	public void parse(IoBuffer message){
		parser.parseBuffer(message);
	}
	
	public void quit(){
		session.removeAttribute("client");
		Main.out.println(new StringBuilder("GameServer : ").append(guid).append(" deconnected").toString());
		session.close(true);
	}

	public IoSession getSession() {
		return session;
	}

	public void setSession(IoSession session) {
		this.session = session;
	}

	public InterParser getParser() {
		return parser;
	}

	public void setParser(InterParser parser) {
		this.parser = parser;
	}
	
	public void send(InterPacket packet){
		session.write(packet.packetOut.flip());
	}
	
	public byte getGuid() {
		return guid;
	}

	public void setGuid(byte guid) {
		this.guid = guid;
	}

	public Type getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = Type.get(type);
	}

	public State getState() {
		return state;
	}

	public void setState(byte state) {
		this.state = State.get(state);
	}

	public Population getPop() {
		return pop;
	}

	public void setPop(byte pop) {
		this.pop = Population.get(pop);
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	public static enum Type {

        CLASSIC((byte)0),
        HARDCORE((byte)1);

        private final byte type;

        private Type(byte type) {
            this.type = type;
        }

        public byte getType() {
            return type;
        }

        public static Type get(byte type) {
            if (type == 1) {
                return HARDCORE;
            }
            return CLASSIC;
        }

    }

    public static enum State {

        OFFLINE((byte)0),
        ONLINE((byte)1),
        SAVING((byte)2);

        private final byte state;

        private State(byte state) {
            this.state = state;
        }

        public byte getState() {
            return this.state;
        }

        public static State get(int state) {
            if(state == 1) {
                return ONLINE;
            } else if(state == 2) {
                return SAVING;
            } else {
                return OFFLINE;
            }
        }

    }

    public static enum Population {

        RECOMMENDED((byte)0),
        AVERAGE((byte)1),
        HIGH((byte)2),
        LOW((byte)3),
        FULL((byte)4);

        private final byte population;

        private Population(byte population) {
            this.population = population;
        }

        public static Population get(int population) {
            if (population == 1) {
                return AVERAGE;
            } else if (population == 2) {
                return HIGH;
            } else if (population == 3) {
                return LOW;
            } else if (population == 4) {
                return FULL;
            }
            return RECOMMENDED;
        }

        public byte getPopulation() {
            return population;
        }
    }

    public static enum Community {

        FRANCOPHONE((byte)0),
        BRITISH((byte)1),
        INTERNATIONALE((byte)2),
        GERMAN((byte)3),
        SPANISH((byte)4),
        RUSSIAN((byte)5),
        BRAZILIAN((byte)6),
        NEERLANDAISE((byte)7),
        ITALIAN((byte)8),
        JAPANESE((byte)10),
        DEBUG((byte)99);

        private final byte community;

        private Community(byte community) {
            this.community = community;
        }

        public static Community get(byte community) {
            if (community == 0) {
                return FRANCOPHONE;
            } else if (community == 1) {
                return BRITISH;
            } else if (community == 3) {
                return GERMAN;
            } else if (community == 4) {
                return SPANISH;
            } else if (community == 5) {
                return RUSSIAN;
            } else if (community == 6) {
                return BRAZILIAN;
            } else if (community == 7) {
                return NEERLANDAISE;
            } else if (community == 8) {
                return ITALIAN;
            } else if (community == 10) {
                return JAPANESE;
            } else if (community == 99) {
                return DEBUG;
            }
            return INTERNATIONALE;
        }

        public byte getCommunity() {
            return community;
        }
    }
	
}
